﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using MolyMade.Http.DataTypes;

namespace MolyMade.Http
{
    public static class Requests
    {
        #region Get
        public static Response Get(string url, IDictionary<string, string> parameters = null, Headers headers = null,
            int connectionTimeout = -1, int readwriteTimeout = -1, CookieCollection cookies = null, string proxy = null, 
            string proxyAcc = null, string proxyPwd = null, string cerPath = null, string cerPwd = null,ICredentials credentials = null, 
            X509CertificateCollection cerCollection = null,  bool allowRedirect = true, bool expect100Continue = true,
            Version protocolVersion = null, IPEndPoint endPoint = null, int maxRedirection = -1, int maxConnection = -1)
        {
            if (protocolVersion == null){protocolVersion = HttpVersion.Version11;}
            return Controller.Controller.Request(new Request()
            {
                Url = url,
                Parameters = parameters,
                HttpMethod = "get",
                Headers = headers,
                ConnectionTimeout = connectionTimeout,
                ReadWriteTimeout = readwriteTimeout,
                Cookies = cookies,
                Proxy = proxy,
                ProxyUserName = proxyAcc,
                ProxyPassword = proxyPwd,
                CerPath = cerPath,
                CerPwd = cerPwd,
                Credentials = credentials,
                X509CertificateCollection = cerCollection,
                AllowAutoRedirect = allowRedirect,
                Expect100Continue = expect100Continue,
                ProtoVersion = protocolVersion,
                IpEndPoint = endPoint,
                MaxAutoRedirection = maxRedirection,
                MaxConnection = maxConnection
            });
        }
        #endregion

        #region Post with byte[]
        public static Response Post(string url,IDictionary<string, string> parameters = null,Headers headers = null,
            int connectionTimeout = -1,int readwriteTimeout = -1,CookieCollection cookies = null,byte[] postBuffer = null,
            string proxy = null,string proxyAcc = null,string proxyPwd = null, string cerPath = null,string cerPwd = null,
            ICredentials credentials = null,X509CertificateCollection cerCollection = null,bool allowRedirect = true,
            bool expect100Continue = true,Version protocolVersion = null,IPEndPoint endPoint = null,int maxRedirection = -1,
            int maxConnection = -1)
        {
            if (protocolVersion == null){protocolVersion = HttpVersion.Version11;}
            return Controller.Controller.Request(new Request()
            {
                Url = url,
                Parameters = parameters,
                HttpMethod = "post",
                Headers = headers,
                ConnectionTimeout = connectionTimeout,
                ReadWriteTimeout = readwriteTimeout,
                Cookies = cookies,
                PostBuffer = postBuffer,
                Proxy = proxy,
                ProxyUserName = proxyAcc,
                ProxyPassword = proxyPwd,
                CerPath = cerPath,
                CerPwd = cerPwd,
                Credentials = credentials,
                X509CertificateCollection = cerCollection,
                AllowAutoRedirect = allowRedirect,
                Expect100Continue = expect100Continue,
                ProtoVersion = protocolVersion,
                IpEndPoint = endPoint,
                MaxAutoRedirection = maxRedirection,
                MaxConnection = maxConnection
            });
        }
        #endregion

        #region Post with String
        public static Response Post(string url, IDictionary<string, string> parameters = null, Headers headers = null,
            int connectionTimeout = -1, int readwriteTimeout = -1, CookieCollection cookies = null, string postString = null,Encoding postEncoding=null,
            string proxy = null, string proxyAcc = null, string proxyPwd = null, string cerPath = null, string cerPwd = null,
            ICredentials credentials = null, X509CertificateCollection cerCollection = null, bool allowRedirect = true,
            bool expect100Continue = true, Version protocolVersion = null, IPEndPoint endPoint = null, int maxRedirection = -1,
            int maxConnection = -1)
        {
            if (protocolVersion == null){protocolVersion = HttpVersion.Version11;}
            if(postEncoding == null) { postEncoding = Encoding.Default;}
            var postBuffer = postEncoding?.GetBytes(postString) ?? Encoding.GetEncoding("GBK").GetBytes(postString);
            return Controller.Controller.Request(new Request()
            {
                Url = url,
                Parameters = parameters,
                HttpMethod = "post",
                Headers = headers,
                ConnectionTimeout = connectionTimeout,
                ReadWriteTimeout = readwriteTimeout,
                Cookies = cookies,
                PostBuffer = postBuffer,
                Proxy = proxy,
                ProxyUserName = proxyAcc,
                ProxyPassword = proxyPwd,
                CerPath = cerPath,
                CerPwd = cerPwd,
                Credentials = credentials,
                X509CertificateCollection = cerCollection,
                AllowAutoRedirect = allowRedirect,
                Expect100Continue = expect100Continue,
                ProtoVersion = protocolVersion,
                IpEndPoint = endPoint,
                MaxAutoRedirection = maxRedirection,
                MaxConnection = maxConnection
            });
        }
#endregion

        public static Response Put()
        {
            return null;
        }

        public static Response Delete()
        {
            return null;
        }

        public static Response Head()
        {
            return null;
        }

        public static Response Options()
        {
            return null;
        }
    }
}
